﻿package org.ilovecode.display {
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class ZoomWindow extends MovieClip {
		
		//{ region Stage elements
		
		public var content:MovieClip;
		public var contentMask:MovieClip;
		
		//} endregion
		
		private var image:DisplayObject;
		private var timer:Timer = new Timer(40);
		private var speed:int = 5;

		public function ZoomWindow() {
			
		}
		
		//{ region Public
		
		public function init(image:DisplayObject):void {
			this.image = image;
			content.addChild(image);
			initListeners();
		}
		
		public function setWindowSize(width:Number, height:Number):void {
			contentMask.width = width;
			contentMask.height = height;
		}
		
		//} endregion
		
		private function initListeners():void {
			this.addEventListener(MouseEvent.MOUSE_OVER, this_MouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, this_MouseOut);
		}
		
		//{ region Handlers
		
		private function this_MouseOver(event:MouseEvent):void {
			timer.addEventListener(TimerEvent.TIMER, moveWindow);
			timer.start();
		}
		
		private function this_MouseOut(event:MouseEvent):void {
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER, moveWindow);
		}
		
		private function moveWindow(event:TimerEvent):void {
			if (isMovingUp()) {
				content.y += speed;
			}
			if (isMovingDown()) {
				content.y -= speed;
			}
			if (isMovingLeft()) {
				content.x += speed;
			}
			if (isMovingRight()) {
				content.x -= speed;
			}
		}
		
		//} endregion
		
		//{ region Moving
		
		/*
		 *  LEFT			RIGHT
		 *   1/4			 1/4
		 *  |	|			|	|
		 *  -------------------------
		 *  |					|	1/4	UP
		 *  |	-------------	|----
		 *  |	|			|	|
		 *  |	|			|	|
		 *  |	|			|	|
		 *  |	-------------	|----
		 *  |					|	1/4 DOWN
		 *  -------------------------
		 */
		
		private function isMovingUp():Boolean {
			if (this.mouseY < (this.contentMask.height * 1 / 4) && isMovingAllowed("up")) {
				return true;
			}
			
			return false;
		}
		
		private function isMovingDown():Boolean {
			if (this.mouseY > (this.contentMask.height * 3 / 4) && isMovingAllowed("down")) {
				return true;
			}
			
			return false;
		}
		
		private function isMovingLeft():Boolean {
			if (this.mouseX < (this.contentMask.width * 1 / 4) && isMovingAllowed("left")) {
				return true;
			}
			
			return false;
		}
		
		private function isMovingRight():Boolean {
			if (this.mouseX > (this.contentMask.width * 3 / 4) && isMovingAllowed("right")) {
				return true;
			}
			
			return false;
		}
		
		private function isMovingAllowed(direction:String):Boolean {
			var isAllowed:Boolean = false;
			switch(direction) {
				case "up":
					if (content.y + speed <= 0) {
						isAllowed = true;
					}
					break;
				case "down":
					if (content.y + content.height - speed > contentMask.height) {
						isAllowed = true;
					}
					break;
				case "left":
					if (content.x + speed <= 0) {
						isAllowed = true;
					}
					break;
				case "right":
					if (content.x + content.width + speed > contentMask.width) {
						isAllowed = true;
					}
					break;
				default:
					throw new Error("Illegal direction");
			}
			
			return isAllowed;
		}
		
		//} endregion

	}
	
}
